package win.smartown.app.weather;

import java.util.ArrayList;
import java.util.List;

import win.smartown.app.base.mvp.BasePresenter;
import win.smartown.app.base.mvp.ILoadingView;
import win.smartown.app.request.RequestListener;
import win.smartown.app.request.RequestManager;
import win.smartown.app.weather.entity.Location;
import win.smartown.app.weather.entity.MultipleData;
import win.smartown.app.weather.entity.WeatherResponse;
import win.smartown.app.weather.entity.forecast.Daily;
import win.smartown.app.weather.entity.forecast.Forecast;
import win.smartown.app.weather.entity.realTime.RealTime;

/**
 * Created by smartown on 2017/5/8 15:14.
 * Desc:
 */
public class Contract {

    public interface View extends ILoadingView {

        public void onDataChanged();

    }

    public static class Presenter extends BasePresenter<View> {

        private Location location;
        private List<MultipleData> dataList;

        public List<MultipleData> getDataList() {
            return dataList;
        }

        public Presenter(View view, Location location) {
            super(view);
            this.location = location;
            dataList = new ArrayList<>();
        }

        public void refresh() {
            dataList.clear();
            getView().onDataChanged();
            getRealTimeWeather();
        }

        private void getRealTimeWeather() {
            getView().getSuperActivity().request(RequestManager.WeatherManager.getRealTimeWeather(location), new RequestListener<WeatherResponse<RealTime>>() {
                @Override
                public void onStart() {
                    getView().startLoading();
                }

                @Override
                public void onFail(Throwable e) {
                    getView().finishLoading();
                }

                @Override
                public void onSuccess(WeatherResponse<RealTime> result) {
                    getView().finishLoading();
                    dataList.add(new MultipleData(1, result.getResult()));
                    getView().onDataChanged();
                    getForecast();
                }
            });
        }

        private void getForecast() {
            getView().getSuperActivity().request(RequestManager.WeatherManager.getForecast(location), new RequestListener<WeatherResponse<Forecast>>() {
                @Override
                public void onStart() {
                    getView().startLoading();
                }

                @Override
                public void onFail(Throwable e) {
                    getView().finishLoading();
                }

                @Override
                public void onSuccess(WeatherResponse<Forecast> result) {
                    getView().finishLoading();
                    final List<Daily.DailyInfo> dailyInfoList = result.getResult().getDaily().getDailyInfo();
                    for (Daily.DailyInfo info : dailyInfoList) {
                        dataList.add(new MultipleData(2, info));
                    }
                    getView().onDataChanged();
                }
            });
        }

    }

}
